package com.sunday.boot3.user.center.openfegin.client.impl;

import com.sunday.boot3.user.center.openfegin.client.PermissionApiClient;
import com.sunday.boot3.user.center.rest.api.PermissionsApi;
import com.sunday.boot3.user.center.rest.vo.permissions.*;
import com.sunday.common.core.constants.DBConstants;
import com.sunday.common.core.constants.LogConstants;
import com.sunday.common.core.remoting.rest.page.PageVO;
import com.sunday.common.core.remoting.rest.response.RestResponse;
import com.sunday.common.core.utils.DateTimeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@RestController
@RequestMapping(PermissionsApi.ACCESS_PATH)
@RequiredArgsConstructor
public class PermissionApiClientImpl {

    private final PermissionApiClient permissionApiClient;

    @GetMapping("/save")
    public RestResponse save() {
        SavePermissionsReqVO savePermissionReqVO = new SavePermissionsReqVO();
        savePermissionReqVO.setName("savePermissions");
        savePermissionReqVO.setCategory(30);
        savePermissionReqVO.setUrl("2333333333");
        savePermissionReqVO.setParentId(1L);
        log.info(LogConstants.request_format, savePermissionReqVO);
        RestResponse response = permissionApiClient.save(savePermissionReqVO);
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    @GetMapping("/remove")
    public RestResponse remove() {
        RemovePermissionsReqVO removePermissionReqVO = new RemovePermissionsReqVO();
        removePermissionReqVO.setIds(page().getData().getRecords().stream().map(PagePermissionsResVO::getId).toList());
        RestResponse response = permissionApiClient.remove(removePermissionReqVO);
        log.info(LogConstants.response_format_ack, response);
        return response;
    }

    @GetMapping("/update")
    public RestResponse update() {
        AtomicInteger pointer = new AtomicInteger(1);
        UpdatePermissionsReqVO updatePermissionsReqVO = new UpdatePermissionsReqVO();
        return page().getData().getRecords().stream().findFirst().map(vo -> {
            updatePermissionsReqVO.setId(vo.getId());
            updatePermissionsReqVO.setName(DateTimeUtils.formatDateTimeMsSplit());
            updatePermissionsReqVO.setUrl(String.valueOf(pointer.getAndIncrement()));
            updatePermissionsReqVO.setParentId(Long.valueOf(pointer.getAndIncrement()));
            log.info(LogConstants.request_format, updatePermissionsReqVO);
            RestResponse response = permissionApiClient.update(updatePermissionsReqVO);
            log.info(LogConstants.response_format_ack, response);
            return response;
        }).get();
    }

    @GetMapping("/page")
    public RestResponse<PageVO<PagePermissionsResVO>> page() {
        PagePermissionsReqVO pagePermissionReqVO = PagePermissionsReqVO.of(1L, 5L);
        log.info(LogConstants.request_format, pagePermissionReqVO);
        RestResponse<PageVO<PagePermissionsResVO>> response = permissionApiClient.page(pagePermissionReqVO);
        log.info(LogConstants.response_format_ack, response);
        return response;
    }


}